package com.ruoyi.web.controller.client;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 参数配置 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/client/config")
public class ClientController extends BaseController
{
    @Autowired
    private ISysConfigService configService;


    /**
     * 根据键值数组获取参数配置
     */
    @GetMapping(value = "/getByParams")
    public AjaxResult getClientParam(String configKeys)
    {
        String [] keys = configKeys.split(",");
        Map<String,String> configKeyMap = new HashMap<>();
        for (String key:keys) {
            configKeyMap.put(key, configService.selectConfigByKey(key));
        }
        return AjaxResult.success(configKeyMap);
    }

    @PostMapping("/saveClientParam")
    public AjaxResult saveClientParam(String data){
        Map<String,String> configKeyMap = JSON.parseObject(data,Map.class);

        for (Map.Entry<String,String> entry:
        configKeyMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            SysConfig config = configService.checkConfigKeyUnique(key);
            config.setConfigValue(value);
            configService.updateConfig(config);
        }
        return AjaxResult.success();
    }


}
